package com.gxa.service.Impl;

import com.gxa.common.R;
import com.gxa.common.ResponseData;
import com.gxa.common.State;
import com.gxa.common.jwt.JwtUtils;
import com.gxa.mapper.TalentPoolMapper;
import com.gxa.pojo.Params_Son;
import com.gxa.pojo.Resume;
import com.gxa.pojo.statistics.Login;
import com.gxa.pojo.statistics.ModifyPwd;
import com.gxa.pojo.statistics.MyResume;
import com.gxa.service.TalentPoolService;
import com.gxa.utils.DateUtils;
import com.gxa.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class TalentPoolServiceImpl implements TalentPoolService {
    @Autowired(required = false)
    private TalentPoolMapper talentPoolMapper;


    //登录接口实现
    @Override
    public ResponseData login(Login login) {
        String re_tel = login.getRe_tel();
        String re_password = login.getRe_password();
        System.out.println(re_tel +" "+ re_password);
        ResponseData r=new ResponseData();
        //判断参数是否为空
        if(re_tel == null || "".equals(re_tel)){
            r.setCode(400);
            r.setMsg("电话不能为空");
            return r;
        }
        if(re_password == null || "".equals(re_password)){
            r.setCode(400);
            r.setMsg("密码不能为空");
            return r;
        }

        //创建token
        String uuid = UUID.randomUUID().toString();
        String token = JwtUtils.createJwt(uuid , "fxr" , re_tel , JwtUtils.TTMILLIS);
        //得到登录对象实体(存储着该对象所有信息)
        String newP = MD5Utils.encrypt(re_password,MD5Utils.MD5_KEY);
        Resume resume = talentPoolMapper.login(re_tel,newP);
        //判断
        if(resume == null){   //如果该对象不存在
            r.setCode(500);
            r.setMsg("登录失败！账号或密码错误");
        }else{  //如果存在
            r.setCode(200);
            r.setMsg("登录成功");
            r.setData(resume);
            r.setToken(token);
        }

        System.out.println(r);
        return r;
    }

    //注册接口实现
    @Transactional
    @Override
    public R register(Resume resume) {
        System.out.println(resume);
        R r=new R();
        //判断参数是否为空
        if(resume.getRe_name() == null || "".equals(resume.getRe_name())){
            r.setCode(400);
            r.setMsg("请输入姓名");
            return r;
        }
        if(resume.getRe_sex() == null || "".equals(resume.getRe_sex())){
            r.setCode(400);
            r.setMsg("请选择性别");
            return r;
        }
        if(resume.getRe_education() == null || "".equals(resume.getRe_education())){
            r.setCode(400);
            r.setMsg("请选择学历");
            return r;
        }
        if(resume.getRe_height() == null || "".equals(resume.getRe_height())){
            r.setCode(400);
            r.setMsg("请填入身高");
            return r;
        }
        if(resume.getRe_tel() == null || "".equals(resume.getRe_tel())){
            r.setCode(400);
            r.setMsg("手机号码不能为空");
            return r;
        }
        if(resume.getRe_password() == null || "".equals(resume.getRe_password())){
            r.setCode(400);
            r.setMsg("密码不能为空");
            return r;
        }

        //查看该手机是否已经登记
        Resume resume2 = talentPoolMapper.queryResumeByTel(resume.getRe_tel());
        //判断
        if(resume2 == null){   //如果该对象不存在
            //执行注册功能
            String newP = MD5Utils.encrypt(resume.getRe_password(),MD5Utils.MD5_KEY);
            resume.setRe_password(newP);    //将经MD5加密后的密码赋给对象
            resume.setRe_status(State.RESUME_REGISTER);     //写入状态  0 已注册未提交
            int result = talentPoolMapper.insertResume(resume);
            if(result == 0){
                r.setCode(505);
                r.setMsg("注册失败，请联系管理员");
            }else{
                r.setCode(200);
                r.setMsg("注册成功");
            }
        }else{      //存在
            r.setCode(500);
            r.setMsg("该手机已经登记过，请勿重复登记");
        }

        System.out.println(r+"\n");
        return r;
    }

    //修改密码接口实现
    @Transactional
    @Override
    public R modifyPwd(ModifyPwd modifyPwd) {
        Integer resume_id = modifyPwd.getResume_id();
        String new_re_password = modifyPwd.getNew_re_password();
        String old_re_password = modifyPwd.getOld_re_password();
        System.out.println(resume_id +" "+ new_re_password+" "+old_re_password);
        R r=new R();
        //判断参数是否为空
        if(resume_id == null || "".equals(resume_id)){
            r.setCode(400);
            r.setMsg("该用户id不能为空");
            return r;
        }
        if(old_re_password == null || "".equals(old_re_password)){
            r.setCode(400);
            r.setMsg("旧密码不能为空");
            return r;
        }
        if(new_re_password == null || "".equals(new_re_password)){
            r.setCode(400);
            r.setMsg("新密码不能为空");
            return r;
        }

        //查询密码是否正确
        String oldP = MD5Utils.encrypt(old_re_password,MD5Utils.MD5_KEY);
        Resume resume = talentPoolMapper.queryResume(resume_id,oldP);
        //判断
        if(resume == null){   //如果该对象不存在
            r.setCode(500);
            r.setMsg("密码错误，请重新输入");
        }else{  //如果存在
            //进行修改密码操作
            String newP = MD5Utils.encrypt(new_re_password,MD5Utils.MD5_KEY);
            int result = talentPoolMapper.updatePwd(resume_id,newP);
            if(result == 0){
                r.setCode(505);
                r.setMsg("修改密码失败，请联系管理员");
            }else{
                r.setCode(200);
                r.setMsg("修改成功");
            }
        }

        System.out.println(r+"\n");
        return r;
    }

    //基本资料接口实现
    @Transactional
    @Override
    public R informationBasic(MyResume resume) {
        //首先查看状态
        int state = resume.getRe_status();
        if(state == 0){     //没资料
            resume.setRe_status(State.RESUME_DATA1);    //genggai只有基本资料
        }
        if(state == -1){     //只有基本资料
            resume.setRe_status(State.RESUME_DATA1);    //genggai只有基本资料
        }
        if(state == -2){     //只有详细资料
            resume.setRe_status(State.RESUME_DATA6);    //genggai只差文件
        }
        if(state == -3){     //只有文件
            resume.setRe_status(State.RESUME_DATA5);    //genggai只差详细资料
        }
        if(state == -4){     //只差基本资料
            resume.setRe_status(State.RESUME_SEE);      //genggai未查看
        }
        if(state == -5){     //只差详细资料
            resume.setRe_status(State.RESUME_DATA5);    //genggai只差详细资料
        }
        if(state == -6){     //只差文件
            resume.setRe_status(State.RESUME_DATA6);    //genggai只差文件
        }


        System.out.println(resume);
        R r=new R();
        //判断参数是否为空
        if(resume.getResume_id() == null || "".equals(resume.getResume_id())){
            r.setCode(400);
            r.setMsg("前端忘记传递id");
            return r;
        }
        if(resume.getRe_name() == null || "".equals(resume.getRe_name())){
            r.setCode(400);
            r.setMsg("请输入姓名");
            return r;
        }
        if(resume.getRe_sex() == null || "".equals(resume.getRe_sex())){
            r.setCode(400);
            r.setMsg("请选择性别");
            return r;
        }
        if(resume.getRe_education() == null || "".equals(resume.getRe_education())){
            r.setCode(400);
            r.setMsg("请选择学历");
            return r;
        }
        if(resume.getRe_birthday() == null){
            r.setCode(400);
            r.setMsg("请填入出生日期");
            return r;
        }
        if(resume.getRe_tel() == null || "".equals(resume.getRe_tel())){
            r.setCode(400);
            r.setMsg("手机号码不能为空");
            return r;
        }
        if(resume.getRe_height() == null || "".equals(resume.getRe_height())){
            r.setCode(400);
            r.setMsg("请填入身高");
            return r;
        }
        if(resume.getRe_nation() == null || "".equals(resume.getRe_nation())){
            r.setCode(400);
            r.setMsg("请选择民族");
            return r;
        }
        if(resume.getRe_native() == null || "".equals(resume.getRe_native())){
            r.setCode(400);
            r.setMsg("请填入籍贯");
            return r;
        }
        if(resume.getRe_applyJob() == null || "".equals(resume.getRe_applyJob())){
            r.setCode(400);
            r.setMsg("请选择应聘岗位");
            return r;
        }
        if(resume.getRe_expectedSalary() == null || "".equals(resume.getRe_expectedSalary())){
            r.setCode(400);
            r.setMsg("请填入期望月薪");
            return r;
        }
        if(resume.getRe_email() == null || "".equals(resume.getRe_email())){
            r.setCode(400);
            r.setMsg("请填入邮箱地址");
            return r;
        }
        if(resume.getRe_refreshDate_string() == null || "".equals(resume.getRe_refreshDate_string())){
            r.setCode(400);
            r.setMsg("前端忘记传递更新时间");
            return r;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date updateDate = null;
        try {
            updateDate = sdf.parse(resume.getRe_refreshDate_string());
            System.out.println(resume.getRe_refreshDate_string());
            System.out.println(resume.getRe_refreshDate_string());
            System.out.println();
            System.out.println();
            System.out.println();
            System.out.println();
            System.out.println(DateUtils.date2String(updateDate,"yyyy-MM-dd HH:mm:ss"));
            System.out.println(DateUtils.date2String(updateDate,"yyyy-MM-dd HH:mm:ss"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        resume.setRe_refreshDate(updateDate);


        int result = 0;
        //根据手机号查询
        Resume resume2 = talentPoolMapper.queryResumeByTel(resume.getRe_tel());
        if(resume2 == null){        //没查出实体
            //修改基本资料
            result = talentPoolMapper.informationBasic(resume);
        }else{      //查出了实体
            if(resume2.getResume_id() == resume.getResume_id()){       //如果查出的实体就是该用户
                //修改基本资料
                result = talentPoolMapper.informationBasic(resume);
            }else{      //不是该用户
                r.setCode(500);
                r.setMsg("该手机已经登记过");
                return r;
            }
        }
        if(result == 0){
            r.setCode(505);
            r.setMsg("修改失败，请联系管理员");
        }else{
            r.setCode(200);
            r.setMsg("修改成功");
        }


        System.out.println(r+"\n");
        System.out.println(resume+"\n");
        return r;
    }

    //详细资料接口实现
    @Transactional
    @Override
    public R informationDetails(MyResume resume) {
        System.out.println(resume);
        R r=new R();
        //首先查看状态
        int state = resume.getRe_status();
        if(state == 0){     //没资料
            resume.setRe_status(State.RESUME_DATA2);    //genggai只有详细资料
        }
        if(state == -1){     //只有基本资料
            resume.setRe_status(State.RESUME_DATA6);    //genggai只差文件
        }
        if(state == -2){     //只有详细资料
            resume.setRe_status(State.RESUME_DATA2);    //genggai只有详细资料
        }
        if(state == -3){     //只有文件
            resume.setRe_status(State.RESUME_DATA4);    //genggai只差基本资料
        }
        if(state == -4){     //只差基本资料
            resume.setRe_status(State.RESUME_DATA4);      //genggai只差基本资料
        }
        if(state == -5){     //只差详细资料
            resume.setRe_status(State.RESUME_SEE);      //genggai未查看
        }
        if(state == -6){     //只差文件
            resume.setRe_status(State.RESUME_DATA6);    //genggai只差文件
        }
        if(resume.getRe_refreshDate_string() == null || "".equals(resume.getRe_refreshDate_string())){
            r.setCode(400);
            r.setMsg("前端忘记传递更新时间");
            return r;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date updateDate = null;
        try {
            updateDate = sdf.parse(resume.getRe_refreshDate_string());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        resume.setRe_refreshDate(updateDate);


        //判断参数是否为空
        if(resume.getResume_id() == null || "".equals(resume.getResume_id())){
            r.setCode(400);
            r.setMsg("前端忘记传递id");
            return r;
        }

        //修改详细资料
        int result = talentPoolMapper.informationDetails(resume);
        if(result == 0){
            r.setCode(505);
            r.setMsg("修改失败，请联系管理员");
        }else{
            r.setCode(200);
            r.setMsg("修改成功");
        }

        System.out.println(r+"\n");
        return r;
    }

    //文件资料接口实现
    @Transactional
    @Override
    public R informationFile(MyResume resume) {
        R r = new R();
        //首先查看状态
        int state = resume.getRe_status();
        if(state == 0){     //没资料
            resume.setRe_status(State.RESUME_DATA3);    //genggai只有文件
        }
        if(state == -1){     //只有基本资料
            resume.setRe_status(State.RESUME_DATA5);    //genggai只差详细资料
        }
        if(state == -2){     //只有详细资料
            resume.setRe_status(State.RESUME_DATA4);    //genggai只差基本资料
        }
        if(state == -3){     //只有文件
            resume.setRe_status(State.RESUME_DATA3);    //genggai只有文件
        }
        if(state == -4){     //只差基本资料
            resume.setRe_status(State.RESUME_DATA4);      //genggai只差基本资料
        }
        if(state == -5){     //只差详细资料
            resume.setRe_status(State.RESUME_DATA5);      //genggai只差详细资料
        }
        if(state == -6){     //只差文件
            resume.setRe_status(State.RESUME_SEE);      //genggai未查看
        }

        if(resume.getRe_refreshDate_string() == null || "".equals(resume.getRe_refreshDate_string())){
            r.setCode(400);
            r.setMsg("前端忘记传递更新时间");
            return r;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date updateDate = null;
        try {
            updateDate = sdf.parse(resume.getRe_refreshDate_string());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        resume.setRe_refreshDate(updateDate);
        System.out.println("service打印  "+resume);

        //修改文件资料
        int result = talentPoolMapper.informationFile(resume);
        if(result == 0){
            r.setCode(505);
            r.setMsg("修改失败，请联系管理员");
        }else{
            r.setCode(200);
            r.setMsg("修改成功");
        }

        System.out.println(r+"\n");
        return r;
    }

    //查询民族接口实现
    @Override
    public R queryNation() {
        R r = new R();
        //得到民族实体集   State.PARAMS_NATION(民族)
        List<Params_Son> params_sonList = talentPoolMapper.queryNation(State.PARAMS_NATION);
        //判断
        if(params_sonList == null){   //如果该对象不存在
            r.setCode(500);
            r.setMsg("查询失败！数据不存在");
        }else{  //如果存在
            r.setCode(200);
            r.setMsg("查询民族成功");
            r.setData(params_sonList);
        }
        System.out.println(r);
        return r;
    }

    //查询职务接口实现
    @Override
    public R queryDuties() {
        R r = new R();
        //得到职务实体集   State.PARAMS_POST(职务)
        List<Params_Son> params_sonList = talentPoolMapper.queryDuties(State.PARAMS_POST);
        //判断
        if(params_sonList == null){   //如果该对象不存在
            r.setCode(500);
            r.setMsg("查询失败！数据不存在");
        }else{  //如果存在
            r.setCode(200);
            r.setMsg("查询职务成功");
            r.setData(params_sonList);
        }
        System.out.println(r);
        return r;
    }

    //查询学历接口实现
    @Override
    public R queryEducation() {
        R r = new R();
        //得到学历实体集   State.PARAMS_DELETE(学历)
        List<Params_Son> params_sonList = talentPoolMapper.queryEducation(State.PARAMS_DELETE);
        //判断
        if(params_sonList == null){   //如果该对象不存在
            r.setCode(500);
            r.setMsg("查询失败！数据不存在");
        }else{  //如果存在
            r.setCode(200);
            r.setMsg("查询学历成功");
            r.setData(params_sonList);
        }
        System.out.println(r);
        return r;
    }

    //主界面调用用户信息
    @Override
    public R resume(MyResume mr) {
        //拿到项目绝对路径
        String uploadDistPath = mr.getRe_refreshDate_string();
        System.out.println(mr);
        String re_tel = mr.getRe_tel();
        R r=new R();
        //判断参数是否为空
        if(mr.getRe_tel() == null || "".equals(mr.getRe_tel())){
            r.setCode(400);
            r.setMsg("前端忘记传递tel");
            return r;
        }

        MyResume resume = talentPoolMapper.queryResumeByTel2(re_tel);
//        //三个路径
//        resume.setRe_selfpic(uploadDistPath+(resume.getRe_selfpic()).substring(1));
//        resume.setRe_jointb(uploadDistPath+(resume.getRe_jointb()).substring(1));
//        resume.setRe_idcardaddress(uploadDistPath+(resume.getRe_idcardaddress()).substring(1));
        //生日
        if(resume.getRe_birthday() == null){
            resume.setRe_birthday_string("还没编辑生日");
        }else{
            Date d=resume.getRe_birthday();
            resume.setRe_birthday_string(DateUtils.date2String(d,"yyyy-MM-dd HH:mm:ss"));
        }
        //更新时间
        if(resume.getRe_refreshDate() == null){
            resume.setRe_refreshDate_string("还没编辑更新时间");
        }else{
            Date d=resume.getRe_refreshDate();
            resume.setRe_refreshDate_string(DateUtils.date2String(d,"yyyy-MM-dd HH:mm:ss"));
        }

        //判断
        if(resume == null){   //如果该对象不存在
            r.setCode(500);
            r.setMsg("查询失败！");
        }else{  //如果存在
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(resume);
        }


        System.out.println(r+"\n");
        return r;
    }
}
